package com.chinaisafe.user.controller;

import com.chinaisafe.gateway.sdk.enums.ClientTypeEnum;
import com.chinaisafe.user.biz.SysTenantBiz;
import com.chinaisafe.user.vo.request.TenantAppAuthReq;
import com.chinaisafe.user.vo.request.TenantChangeStatusReqVO;
import com.chinaisafe.user.vo.response.LoginTenantTreeVO;
import com.chinaisafe.user.vo.response.LoginTenantVO;
import com.chinaisafe.user.vo.response.SysTenantAppRespVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import com.chinaisafe.base.common.annotation.OperationLog;
import com.chinaisafe.base.common.enums.BusinessTypeEnum;
import com.chinaisafe.base.common.vo.Answer;
import com.baomidou.mybatisplus.core.metadata.IPage;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.chinaisafe.user.vo.request.SysTenantReqVO;
import com.chinaisafe.user.vo.response.SysTenantRespVO;
import org.springframework.validation.annotation.Validated;
import com.chinaisafe.user.service.SysTenantService;
import org.springframework.web.bind.annotation.RestController;
import com.chinaisafe.base.common.validated.AddGroup;
import com.chinaisafe.base.common.validated.UpdateGroup;
import java.util.List;
import java.util.Map;

/**
 * SysTenantController
 * Description：租户接口层
 *
 * @author xuefu.shu
 * @date  2024-07-12 10:22:08
 */
@RestController
@RequestMapping("/tenant")
@Tag(name = "租户接口层", description = "租户接口层")
public class SysTenantController {

    @Autowired
    private SysTenantService sysTenantService;

    @Resource
    private SysTenantBiz sysTenantBiz;

    @GetMapping("/pageList")
    @Operation(summary = "获取租户分页列表", description = "获取租户分页列表")
    public Answer<IPage<SysTenantRespVO>> pageList(SysTenantReqVO sysTenantReqVO) {
        IPage<SysTenantRespVO> pageList = sysTenantService.getPageList(sysTenantReqVO);
        return Answer.success("获取成功", pageList);
    }

    @PutMapping("/changeStatus")
    public Answer<String> changeStatus(@RequestBody TenantChangeStatusReqVO changeStatusReqVO) {
        sysTenantService.changeStatus(changeStatusReqVO);
        return Answer.success("修改成功");
    }

    @PostMapping()
    @Operation(summary = "新增租户", description = "新增租户")
    @OperationLog(title = "租户-新增", businessType = BusinessTypeEnum.INSERT)
    public Answer<SysTenantRespVO> add(@RequestBody @Validated(AddGroup.class) SysTenantReqVO sysTenantReqVO) {
        SysTenantRespVO sysTenantRespVO = sysTenantService.add(sysTenantReqVO);
        return Answer.success("保存成功", sysTenantRespVO);
    }

    @PutMapping()
    @Operation(summary = "根据id编辑租户", description = "根据id编辑租户")
    @OperationLog(title = "租户-根据id编辑", businessType = BusinessTypeEnum.UPDATE)
    public Answer<SysTenantRespVO> update(@RequestBody @Validated(UpdateGroup.class) SysTenantReqVO sysTenantReqVO) {
        SysTenantRespVO sysTenantRespVO = sysTenantService.update(sysTenantReqVO);
        return Answer.success("修改成功", sysTenantRespVO);
    }

    @GetMapping("/{sysTenantId}")
    @Operation(summary = "根据id获取租户", description = "根据id获取租户")
    public Answer<SysTenantRespVO> getDetailById(@PathVariable Long sysTenantId) {
        SysTenantRespVO sysTenantRespVO = sysTenantService.getDetailById(sysTenantId);
        return Answer.success("获取成功", sysTenantRespVO);
    }

    @DeleteMapping()
    @Operation(summary = "根据id删除租户", description = "根据id删除租户")
    @OperationLog(title = "租户-根据id删除", businessType = BusinessTypeEnum.DELETE)
    public Answer<Void> deleteByIds(@RequestParam("ids") Long[] ids) {
        sysTenantService.deleteByIds(ids);
        return Answer.success("删除成功");
    }

    @GetMapping("/appList")
    @Operation(summary = "获取所有的应用", description = "获取所有的应用")
    public Answer<Map<ClientTypeEnum,List<SysTenantAppRespVO>>> queryList(String tenancyCode) {
        Map<ClientTypeEnum,List<SysTenantAppRespVO>> result = sysTenantBiz.queryList(tenancyCode);
        return Answer.success("获取成功", result);
    }


    @PostMapping("appAuthorize")
    @Operation(summary = "应用授权", description = "apps参数为授权的有序的app集合,未授权的不要传进来")
    @OperationLog(title = "应用授权", businessType = BusinessTypeEnum.INSERT)
    public Answer<SysTenantRespVO> appAuthorize(@RequestBody @Validated(UpdateGroup.class) TenantAppAuthReq tenantApp) {
        sysTenantBiz.appAuthorize(tenantApp);
        return Answer.success("保存成功");
    }


    @GetMapping("/user/tree")
    @Operation(summary = "获取用户拥有租户树", description = "获取用户拥有租户树，包含所在租户和上级")
    public Answer<LoginTenantVO> tenantUserTree() {
        return Answer.success("获取成功", sysTenantService.tenantUserTree());
    }

}

